package.path = '/usr/local/Cellar/openresty/1.13.6.1/lualib/resty/?.lua;/opt/soft/nginx/lualib/resty/?.lua;'
package.cpath = '/usr/local/Cellar/openresty/1.13.6.1/lualib/?.so;/opt/soft/nginx/lualib/?.so;;'


local cjson = require "cjson"

local cdp_key = ngx.var.cookie_cdp_key
local cdp_user = ngx.var.cookie_cdp_user


function render(success, message, data)
	local res = {success=success, message=message, data=data}
	return cjson.encode(res)
end
function renderOK(message, data)
	return render(true, message, data)
end
function renderError(message, data)
	return render(false, message)
end

local result = renderError('未登录!');

if (cdp_key and cdp_user) then
    local checkUrl = '/sso/check?cdp_key='..cdp_key..'&cdp_user='..cdp_user
	local res = ngx.location.capture(checkUrl)
	ngx.log(ngx.INFO, cjson.encode(res))
	if res.status==200 then
		local resbody = cjson.decode(res.body)
		if resbody.result==1 then
			result = renderOK('已登录!', cjson.decode(resbody.data))
		else
			result = renderError('未登录!', resbody.data)
		end
	else
		result = renderError(cjson.encode(res), null)
	end	
end

ngx.say(result)
